package com.jinboe.cms.doc.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class EntityUtils {
	//调用setter
	public static void invokeSetterMethod(Object obj , String attribute, Object value) throws Exception{  
        Class clazz = obj.getClass();  
        Field field = clazz.getDeclaredField(attribute);  
        Class paramType = field.getType();  
        Method method = clazz.getMethod(getMethodName(false,attribute), paramType);  
        method.invoke(obj, value);  
          
    }
	//调用gettet
	private Object invokeGetterMethod(Object obj, String attribute)throws Exception { 
	        Class clazz = obj.getClass();  
	        System.out.println(getMethodName(true,attribute));  
	        Method method = clazz.getMethod(getMethodName(true,attribute));
	        return method.invoke(obj);  
	    }  
	private static String getMethodName(boolean flag ,String attribute) {  
        String firstElemntOfAttribute = attribute.substring(0, 1).toUpperCase();  
        String restElementOfAttrute = attribute.substring(1);  
        return (flag ? "get"  : "set")  +  firstElemntOfAttribute + restElementOfAttrute;  
    } 
	// 用来遍历对象属性和属性值
	public static List<String> readClassAttr(Object tb) {
		List<String> fieldNamelist = new ArrayList();
		Field[] fields = tb.getClass().getDeclaredFields();
		String keyList = "";
		String valueList = "";
		for (Field field : fields) {
			field.setAccessible(true);
			try {
				if (field.get(tb) != null
						&& !"".equals(field.get(tb).toString())) {
					keyList += "," + field.getName();
					fieldNamelist.add(field.getName());
					System.out.println(field.getName());
				}
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return fieldNamelist;
	}

}
